/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.uha.ensisa.kfetmanager.customer;

import fr.uha.ensisa.kfetmanager.category.CustomerCategory;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author JC
 */
@Entity
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;
    private String nickname;
    private boolean cotisant;
    private boolean gerantKfet;
    private double balance;

    @ManyToOne(fetch = FetchType.EAGER)
    private CustomerCategory category;

    @Temporal(TemporalType.DATE)
    private Date creationDate;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance){
        this.balance = balance;
    }

    public boolean isCotisant() {
        return cotisant;
    }

    public void setCotisant(boolean cotisant) {
        this.cotisant = cotisant;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public boolean isGerantKfet() {
        return gerantKfet;
    }

    public void setGerantKfet(boolean gerantKfet) {
        this.gerantKfet = gerantKfet;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public CustomerCategory getCategory() {
        return category;
    }

    public void setCategory(CustomerCategory category) {
        this.category = category;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Customer)) {
            return false;
        }
        Customer other = (Customer) object;
        // First compare last names
        if (!other.getLastName().equals(this.getLastName())) return false;
        // Then compare nicknames
        if (!other.getNickname().equals(this.getNickname())) return false;

        return true;
    }

    @Override
    public String toString() {
        StringBuffer tmp = new StringBuffer();
        tmp.append(firstName + " ");
        // Only append nickname if it is used, i.e. not equal to last name
        if (nickname != null && !nickname.equals(lastName)) tmp.append("\"" + nickname + "\" ");
        tmp.append(lastName + ", ");
        tmp.append(String.valueOf(balance) + " EUR");

        return tmp.toString();
    }

}
